package personnel.department.pages;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.hibernate.Query;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import personnel.department.dao.HibernateUtils;
import personnel.department.dao.entity.Employee;

public class QueryResultComposite extends Composite {

	private Label docdors;
	private Label middlePersons;
	private Label juniorPersons;
	private Map<String, Label> labelByPost;

	public QueryResultComposite(Composite parent, int style) {
		super(parent, style);
		createContent();
	}

	private void createContent() {
		setBackground(JFaceColors.getBannerBackground(getDisplay()));
		GridLayoutFactory.swtDefaults().applyTo(this);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
		
		Label title = new Label(this, SWT.NONE);
		title.setBackground(this.getBackground());
		GridDataFactory.swtDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(title);
		title.setText("Информация по сотрудникам");
		
		docdors = new Label(this, SWT.NONE);
		docdors.setBackground(this.getBackground());
		
		middlePersons = new Label(this, SWT.NONE);
		middlePersons.setBackground(this.getBackground());
		
		juniorPersons = new Label(this, SWT.NONE);
		juniorPersons.setBackground(this.getBackground());
		clear();
//		Query query = HibernateUtils.getSession().createQuery("SELECT t.post FROM EmployeeType AS t");
//		List<String> posts = query.list();
//		labelByPost = Maps.newHashMap();
//		posts.forEach(post -> {
//			Label label = new Label(this, SWT.NONE);
//			label.setText(post);
//			label.setBackground(this.getBackground());
//			labelByPost.put(post, label);
//		});
	}
	
	public void updateInfo(List<Employee> employees) {
		int ds = 0;
		int mp = 0;
		int jp = 0;
		for (Employee emp : employees) {
			switch (emp.getType().getType()) {
			case "Врач":
				ds++;
				break;
			case "Средний медицинский персонал":
				mp++;
				break;
			case "Младший медицинский персонал":
				jp++;
				break;
			}
		}
		setValue(docdors, ds);
		setValue(middlePersons, mp);
		setValue(juniorPersons, jp);
		
//		for (String post : labelByPost.keySet()) {
//			int count = (int)employees.stream()
//			.filter(empl -> empl.getType().getPost().equals(post))
//			.count();
//			setValue(labelByPost.get(post), count);
//		}
		
	}
	
	public void clear() {
		docdors.setText("Врачи: ");
		middlePersons.setText("Средний медицинский персонал: ");
		juniorPersons.setText("Младший медицинский персонал: ");
		docdors.pack();
		middlePersons.pack();
		juniorPersons.pack();

	}
	
	
	private void setValue(Label label, int value) {
		setValue(label, String.valueOf(value));
	}
	
	private void setValue(Label label, String value) {
		String currentValue = label.getText();
		int endIndex = currentValue.indexOf(":");
		String text = currentValue.substring(0, endIndex);
		label.setText(text + ": " + value);
		label.pack();
	}

}
